'use strict';
/* Blog Controllers */
define( function(){
	var BlogCtrl = ['$state','$scope', '$http', '$stateParams','ztree',function($state, $scope, $http,$stateParams, ztree){
		$scope.blog = {};
		//编辑博客
		if($stateParams.blog_id){
			
			$http({	method:'post',
				url:"blog/findById",
				params:{blog_id: $stateParams.blog_id, edit:true}
			}).success(function(result){
				$scope.blog = result.blog;
			}).error(function(){
				alert("网络连接失败");
			});
			
		}
		
		init();
		// 初始化
		function init(){
			//获取用户博客类别
			$http({	method:'post',
				url:"blog_category/findListByUid",
				params:{}
			}).success(function(result){
				$scope.blog_categorys = result.datas;
				
				angular.forEach(result.datas, function(cate){
					if(cate.id = $scope.blog.blog_cate_id){
						$scope.blogCate = cate;   // 博客分类
						return false;
					}
					
				});
			}).error(function(){
				alert("网络连接失败");
			});
			
			//获取系统博客类别
			$http({	method:'post',
				url:"category/findCatesByType",
				params:{type: "blog"}
			}).success(function(result){
				
				var option = {id: 'cates_tree', data: result.data};
				ztree.init(option);     // ztree 服务
				
			}).error(function(){
				alert("网络连接失败");
			});
		}
		// 添加博客
		$scope.addBlog = function(){
			$scope.blog.category_id = ztree.getSelectNodeId();
			$scope.blog.blog_cate_id = $scope.blogCate.id;
			$http({	method:'post',
				url:"blog/addOrUpdate",
				params:$scope.blog
			}).success(function(result){
				if(result.status == 1){
					$state.go("space.blog_list");
				}else{
					alert(result.msg);
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
	}];
	
	var BlogListCtrl = ['$state','$scope', '$http','$stateParams', function($state, $scope, $http, $stateParams){
		$http({	method:'post',
			url:"blog/findByCateId",
			params:{user_id:$stateParams.user_id, blog_cate_id: $stateParams.blog_cate_id}
		}).success(function(result){
			$scope.blogs = result.blogs;
		}).error(function(){
			alert("网络连接失败");
		});
		
		$scope.deleteBlog = function(blog_id){
			
			if(!confirm("文章删除后无法恢复, 确认删除?"))
				return;
			
			$http({	method:'post',
				url:"blog/deleteById",
				params:{blog_id: blog_id}
			}).success(function(result){
				if(result.status == 1){
					$state.go("space.blog_list", $state.params, { reload: true });
				}else {
					alert(result.msg);
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
	}];
	
	var BlogInfoCtrl = ['$stateParams','$scope', '$http', function($stateParams, $scope, $http){
		
		$http({	method:'post',
			url:"blog/findById",
			params:{blog_id: $stateParams.blog_id}
		}).success(function(result){
			$scope.blog = result.blog;
			$scope.comments = result.comments;
			$scope.is_favorite = result.isFavorite;
		}).error(function(){
			alert("网络连接失败");
		});
		
		$scope.deleteBlog = function(blog_id){
			
			if(!confirm("文章删除后无法恢复, 确认删除?"))
				return;
			
			$http({	method:'post',
				url:"blog/deleteById",
				params:{blog_id: blog_id}
			}).success(function(result){
				if(result.status == 1){
					$state.go($state.current.name, $state.params, { reload: true });
				}else {
					alert(result.msg);
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};	
		
		$scope.comment = {};
		$scope.commentBlog = function(blog){
			var content = $("#blog_comment").text();
			if(!content){
				alert("请填写说说");return;
			}
			$scope.comment.type_id = blog.id;
			$scope.comment.type = "3";    		// 博客评论
			$scope.comment.receiver_id = blog.user_id;
			$scope.comment.content = content;
			
			$http({	method:'post',
				url:"comment/addComment",
				params:$scope.comment
			}).success(function(result){
				alert(result.msg);
				if(result.status == 1){
					$scope.comments.push(result.comment);
					$("#blog_comment").html("");
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
		
		// 收藏博客
		$scope.favoriteBlog = function(blog){
			var favorite = {type_id: blog.id, type:'blog', title:blog.title, href:"space.blog_info({user_id:"+blog.user_id+",blog_id:"+blog.id+"})"};
		
			$http({	method:'post',
				url:"favorite/addFavorite",
				params:favorite
			}).success(function(result){
				if(result.status == 1){
					$scope.blog.is_favorite = true;
				}else{
					alert(result.msg);
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
		
		// 取消收藏
		$scope.cancelFavorite = function(blog){
			var params = {type_id : blog.id, type:'blog'};
			$http({	method:'post',
				url:"favorite/cancelFavorite",
				params:params
			}).success(function(result){
				if(result.status == 1){
					$scope.blog.is_favorite = false;
				}else{
					alert(result.msg);
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
	}];
	
	return {add: BlogCtrl, list: BlogListCtrl, info:BlogInfoCtrl};
});